from scrapy.spiders import Spider
from scrapy.selector import Selector
from scrapy.http import Request

from qunar.items import QunarItem


class QunarSpider(Spider):
    name = "qunar"
    allowed_domains = ["qunar.com"]
    start_urls = \
        ['http://travel.qunar.com/p-cs299914-beijing-jingdian-1-%s' %
         page_id for page_id in range(1, 169)]

    def parse(self, response):
        selector = Selector(response)
        spot = selector.xpath('//div[@class="listbox"]/ul/li')[0]
        detail_url = spot.xpath('//div[@class="ct"]/div[1]/a/@href').extract()
        for url in detail_url:
            yield Request(url, callback=self.parse_spot)

    def parse_spot(self, response):
        selector = Selector(response)
        item = QunarItem()
        item['url'] = response.url
        item['name'] = selector.xpath('//*[@id="js_mainleft"]/div[3]/h1/text()').extract()[0]
        item['score'] = selector.xpath('//*[@id="js_mainleft"]/div[4]/div/div[2]/div[1]/div[1]/span[1]/text()').extract()[0]
        item['comment_num'] = selector.xpath('//*[@id="detail-nav"]/ul/li[@class="e_nav_last"]/div/span/span/text()').extract()[0]
        address = selector.xpath('//*[@id="gs"]/div[2]/div/table/tr/td[1]/dl[1]/dd')
        if len(address) > 0:
            item['address'] = address[0].xpath('string(.)').extract()[0].strip().replace(' ', '').replace('\t', '').replace('\n', '')
        else:
            item['address'] = item['name']
        description = selector.xpath('//*[@id="gs"]/div[1]')[0]
        item['description'] = description.xpath('string(.)').extract()[0].strip().replace(' ', '').replace('\t', '').replace('\n', '')
        ticket_price = selector.xpath('//*[@id="mp"]/div[2]')
        if len(ticket_price) > 0:
            item['ticket_price'] = ticket_price[0].xpath('string(.)').extract()[0].strip().replace(' ', '').replace('\t', '').replace('\n', '')
        else:
            item['ticket_price'] = 'no ticket'
        available_time = selector.xpath('//*[@id="gs"]/div[2]/div/table/tr/td[2]/dl[1]/dd')
        if len(available_time) > 0:
            item['available_time'] = available_time[0].xpath('string(.)').extract()[0].strip().replace(' ', '').replace('\t', '').replace('\n', '')
        else:
            item['available_time'] = 'no limit'
        suggest_time = selector.xpath('//*[@id="js_mainleft"]/div[4]/div/div[2]/div[1]/div[2]/div[@class="time"]')
        if len(suggest_time) > 0:
            item['suggest_time'] = suggest_time[0].xpath('string(.)').extract()[0].strip().replace(' ', '').replace('\t', '').replace('\n', '')
        else:
            item['suggest_time'] = 'none'
        return item




